$(function(){
     	
	/*计算下半部分的高度*/
	$("#tf-main").height($(window).height()-$(".tf-head").height());
     
    //退出按钮
    $("#logoutBtn").on("click",function(){
    	$.ajax({
    		url : LocalCache.logoutActionUrl,
		 	type :'POST',
		 	dataType : "json",
		 	contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
		 	success:function(data){
		 		 window.top.location.href = LocalCache.loginPageUrl;
		 	}		
		 });
    });
    
    //logo图标点击
    $("#logoLinkBtn").click(function(){
    	  linkHomePage(LocalCache.homePageUrl);
    });
    
    //全屏
    $("#fullScreenBtn").on("click",function(){
        if($("#tf-main").css("paddingLeft").split("px")[0] == 0){
        	showLeftMenu();
        }else{
        	hideLeftMenu();
        }
    });
    
    //菜单按钮
    $("#menuConfigBtn").click(function(){
    	
    	var lastAccessMenuItem = {"linkUrl" :  LocalCache.menuConfigPageUrl};
    	
    	//标志位特殊的链接
    	lastAccessMenuItem.special = true;
    	
    	//缓存
    	$.cookie("lastAccessMenuItem" , JSON.stringify(lastAccessMenuItem));
    	
    	 //去除所有菜单项的高亮
    	 removeAllMenuItemHighlight();
    	 
    	 //左边菜单隐藏
    	 hideLeftMenu(true);
    	 redirectLocal(LocalCache.menuConfigPageUrl);
    });
    
    /*框架界面初始化*/
    initFrameInterface();
    
});

/*框架界面初始化*/
function initFrameInterface(){
	
	//初始化框架皮肤
	initFrameSkin();
	
	 //展示页面
    $('body').show();
	
}

//初始化框架皮肤
function initFrameSkin(){
	
	if(!LocalCache.skin) return;
    	
	//若皮肤是乱设置的,则不起作用
	var skin = $.trim(LocalCache.skin);
	if(skin == "" || skin.indexOf("-") == -1) return;
	
	//设置顶部菜单栏皮肤
	var colorArray = skin.split("-");
	var startColor = colorArray[0];
	var endColor = colorArray[1];
	$(".tf-head").css("background" , startColor)
	.css("filter" , "alpha(opacity=100 finishopacity=50 style=1 startx=0,starty=0,finishx=0,finishy=150) progid:DXImageTransform.Microsoft.gradient(startcolorstr="+startColor+",endcolorstr="+endColor+",gradientType=0)")
	.css("-ms-filter" , "alpha(opacity=100 finishopacity=50 style=1 startx=0,starty=0,finishx=0,finishy=150) progid:DXImageTransform.Microsoft.gradient(startcolorstr="+startColor+",endcolorstr="+endColor+",gradientType=0)")
	.css("background" , "-moz-linear-gradient(left, "+startColor+", "+endColor+")")
	.css("background" , "-webkit-linear-gradient(left,"+startColor+","+endColor+")")
	.css("background" , "-o-linear-gradient(left, "+startColor+", "+endColor+")");
    	
}

//主页代码逻辑块
var IndexPage = {
		
		//修改密码
		modifyPassword : function(){
			
			var oldPassword = $.trim($("#mpwd_oldPassword").val());
			
			var newPassword = $.trim($("#mpwd_newPassword").val());
			
			var confirmNewPassword = $.trim($("#mpwd_confirmNewPassword").val());
	    	
	    	if(oldPassword == ""){
	    		new $.zui.Messager('原密码不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	if(newPassword == ""){
	    		new $.zui.Messager('新密码不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	if(newPassword != confirmNewPassword){
	    		new $.zui.Messager('新密码和确认密码不一致', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
			
	    	var params = {"id" : $("#userName").attr("userId") , "oldPassword" : hex_md5(oldPassword) , "newPassword" : hex_md5(newPassword)};
			
	    	var requestUrl = "/sysUser/modifyPassword";
			
			ajaxRequestByJSONParams(requestUrl , params , function(rst){
				
				if(rst.code != 200){
					new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
					return;s
				}
				
				$(".closeWindowBtn").click();
				
				new $.zui.Messager(rst.message, {icon:'times',type: 'success',placement: 'center',time:'2000'}).show();
				
			});
			
		},
		
		//弹出新增窗口
	    popEditSysUserWindow : function(index){
	     	
	    	var currentUpdateRecord = LocalCache.userInfo;
	    	
	    	//显示弹窗
	     	$('#modifyPersonInfoWindow').modal();
	     	
	     	for(var key in currentUpdateRecord){
	     		
	     		var value = currentUpdateRecord[key];
	     		
	     		if(key == "birthday" && value && value != "" && value != null){
	     			$("#mdperson_" + key).val(CommonUtil.getDateYmd(new Date(value)));
	     		}else{
	     			$("#mdperson_" + key).val(value);
	     		}
	     		
	     	}
	   },
		
	  //获取修改个人用户信息的表单参数
      getUpdateSysUserFormParams : function(){
	    	
	    	var params = {};
	    	
			params.userName =  $.trim($("#mdperson_userName").val());
			params.email =  $.trim($("#mdperson_email").val());
			params.phoneNumber =  $.trim($("#mdperson_phoneNumber").val());
			params.gender =  $.trim($("#mdperson_gender").val());
			params.birthday =  $.trim($("#mdperson_birthday").val());
	    	params.id = $("#userName").attr("userId");
			
	    	return params;
	    	
	    },
	    
	    //校验新增的参数
	    validateUpdateSysUserFormParams : function(params){
	    	
	    	if(params.userName == ""){
	    		new $.zui.Messager('姓名不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return;
	    	}
	    	
	    	//校验的逻辑自己填写,返回true和false,可以弹出一些提示
	    	return true;

	    },
		
		//修改用户信息
	    modifyPersonInfo : function(){
			
			var params = this.getUpdateSysUserFormParams();
	    	
	    	//校验不通过,直接返回
	    	if(!this.validateUpdateSysUserFormParams(params)) return;
	    	
	    	$.ajax({
			 	url : '/sysUser/modifyPersonInfo',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	
			 	beforeSend : function(){
			 		$("#addSysUserBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$("#addSysUserBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(data){
			 		
			 		if(data.code == 200){
			 			new $.zui.Messager('修改成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 		    getLoginInfo();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(data.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
			
		},
		
		//弹出系统动态窗口
		popSysMsgWindow : function(){
			
			var pageNum = 1;
			var pageSize = 20;
			
			this.showOperateTrace(pageNum , pageSize);
			
			$("#sysMsgWindow").modal();
			
		}, 
		
		//加载更多系统消息
		loadMoreSysMsg : function(){
			
			var pageNum = $("#sysMsgWindow").attr("pageNum");
			
			if(!pageNum)
				return;
			
			this.showOperateTrace(parseInt(pageNum) + 1 , 20);
		},
		
		//展示操作轨迹
		showOperateTrace : function(pageNum , pageSize){
			
			var requestUrl = "/operateTrace/findOperateTraceList";
			
			var params = {"pageNum" : pageNum , "pageSize" : pageSize};
			
			ajaxRequestAsyncByJSONParams(requestUrl , params , function(rst){
				
				if(rst.code != 200){
					return;
				}
				
				//保存页码
				$("#sysMsgWindow").attr("pageNum" , params.pageNum)
				
				var data = rst.data.list;
				
				var txt = "";
				
				for(var i =  0 ; i < data.length ; i ++){
					
					var item = data[i];
					
					var operateType = CommonUtil.fillBlank(item.operateType);
					var operateTypeTxt = '<span style="margin-left:5px;color:#838A9D;">【'+operateType+'】</span>';
					
					var businessType = CommonUtil.fillBlank(item.businessType);
					var businessTypeTxt = '<span style="margin-left:5px;">'+businessType+'</span>';
					
					var businessNo = CommonUtil.fillBlank(item.businessNo);
					var businessNoTxt = businessNo == "" ? "" : '<span style="margin-left:5px;color:gray;border:1px solid #ccc;display:inline-block;padding:0px 8px;">'+businessNo+'</span>';
					
					var title = CommonUtil.fillBlank(item.title);
					var titleTxt = 	'<span style="margin-left:5px;">'+title+'</span>';
					
					txt +='<p style="overflow-x:auto;">'+
								'<span>【'+CommonUtil.getDateYmdhms(new Date(item.operateTime))+'】</span>'+
								'<span>'+CommonUtil.fillBlank(item.operateUserName)+'</span>'+ 
								 operateTypeTxt + businessTypeTxt + businessNoTxt + titleTxt + 
							'</p>';
					
				}
				
				if(params.pageNum == 1){
					
					if(data.length == 0){
						var noMsgTxt = '<div class="noMsg"  ><i class="icon icon-chat-line">&nbsp;暂无数据</i></div>';
						$("#sysMsgFormLayer").html(noMsgTxt);
					}else{
						$("#sysMsgFormLayer").html(txt);
					}
					
				}else{
					$("#sysMsgFormLayer").append(txt);
				}
			
				
			});
			
		}
		
}

